<?
// Set default parameters
	
	if (empty($start)) { $start = 0; }
	if (empty($page)) { $page = 1; }
	if (empty($sort)) { $sort = "ProviderNameFull"; }
	if (empty($limit)) { $limit = 20; }
	if (!empty($page_current)) {
		$page = $page_current;
		$start = $limit*$page_current;
	}
	
	//$sort = "Provider_PID";

	// If disability_PID
	if (!empty($disability_PID) && ($disability_PID != "Select a Disability...") && ($disability_PID != 27)) {
		$dJOIN  = "LEFT JOIN oa_btb_Disabilities D ON P.Provider_PID = D.Provider_ID";
		$dWHERE = " D.DisabilityType_ID = '$disability_PID' ";
		$dSearch = 1;
		$searchKeywordAND = " AND ";
		$searchZipAND     = " AND ";
	} else {
	    $dJOIN  = null;
	    $dWHERE = null;
	}
	
	// If product_PID
	if (!empty($product_PID) && ($product_PID != "Select a Service Category...")) {
		$pJOIN  = " LEFT JOIN oa_btb_Products PR ON P.Provider_PID = PR.Provider_ID ";
		$pWHERE = " PR.ProductType_ID = '$product_PID' ";
		$pSearch = 1;
		$searchKeywordAND = " AND ";		
	} else {
	    $pJOIN  = null;
		$pWHERE = null;
	}
	if (!empty($dSearch) && !empty($pSearch)) {
		$searchAND = " AND ";
	}	
	
	// If Keyword
	if (!empty($keyword) && ($keyword != "Keyword...")) {
		$keywordSearch = $searchKeywordAND." (
			P.ProviderNameFull LIKE '%$keyword%' OR
			P.ProviderClass    LIKE '%$keyword%' OR
			P.ProviderDesc     LIKE '%$keyword%' OR
			P.ProviderMission  LIKE '%$keyword%' OR
			P.ProviderNotes    LIKE '%$keyword%'
			)
		";		
	}	
	if (!empty($search)) { $searchAND2 = " AND "; }
	
	// Just get total rows first
	$sql = "SELECT Provider_PID
			FROM oa_btb_Providers P
				$dJOIN
				$pJOIN
			";
			
	if ($visible == "0") { $sql .= " WHERE IsVisible = '$visible' "; }
	elseif (!empty($search)) {
	    $sql .= " WHERE
				$dWHERE
			$searchAND
				$pWHERE
			$keywordSearch
			";
	}
    $result = mysql_db_query($dbname,$sql);
    $rows = mysql_num_rows($result);
	$pages = ceil($rows/$limit);
		
	// Now get data
	$sql = "SELECT *
			FROM oa_btb_Providers P
				$dJOIN
				$pJOIN";
				
    //if (isset($visible)) { $sql .= " WHERE IsVisible = '$visible' "; }
    if ($visible == "0") { $sql .= " WHERE IsVisible = '$visible' "; }
	elseif (!empty($search)) {
	    $sql .= " WHERE
				$dWHERE
			$searchAND
				$pWHERE
			$keywordSearch ";
	}
   	$sql .= "ORDER BY
				$sort ASC
			LIMIT
				$start, $limit";
	$result = mysql_db_query($dbname,$sql);

	$linkString = "search=$search&viewAll=".$viewAll."&sort=".$sort."&visible=".$visible."&disability_PID=".urlencode($disability_PID)."&product_PID=".urlencode($product_PID)."&keyword=".urlencode($keyword)."&limit=".$limit;
	
	if (empty($keyword)) { $keyword = "Keyword..."; }

?>